package com.lzw.service.impl;

import com.lzw.dao.StudentDao;
import com.lzw.pojo.Student;
import com.lzw.service.StudentService;
import com.lzw.until.MyBatisUntil;
import com.lzw.until.PageUntil;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Transactional//开启声明式事务
@Service("ss")
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentDao sd;
    @Override
    //传播行为和隔离级别，增删改不用隔离级别
    @Transactional(propagation = Propagation.SUPPORTS, isolation = Isolation.SERIALIZABLE)
    public List<Student> getAllStu() {
        //获取SQL对话
       /* SqlSession session=MyBatisUntil.getSession();
        //接口的代理对象
        StudentDao sd=session.getMapper(StudentDao.class);*/
        List<Student> list = sd.getAllStu();
        return list;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean delAll(String ids) {
        int num = sd.delAll(ids);
        if(num > 0){
            return true;
        }
        return false;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean addStu(Student student) {
        int num=sd.addStu(student);
        if(num>0){
            return true;
        }else
        return false;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS, isolation = Isolation.SERIALIZABLE)
    public Student getStuByNo(Integer stuNo) {
      Student student=sd.getStuByNo(stuNo);
        return student;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean updateStu(Student stu) {
      int num=sd.updateStu(stu);
        if(num>0){
            return true;
        }else
            return false;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS, isolation = Isolation.SERIALIZABLE)
    public PageUntil getStuPage(Map<String, Object> map,PageUntil page) {
        //执行统计数量功能
        int num=sd.getStuCount(map);
        //执行分页
        List<Student> stus=sd.getStuPage(map);
        //把总数量和每页现实的数据赋给page
        page.setPageCount(num);
        page.setList(stus);
        return page;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean delStu(Integer stuNo) {
               int num=sd.delStu(stuNo);
        if(num>0){
            return true;
        }else
            return false;

    }

}

